<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	protected $_title = '';
	protected $_school_year_id = '';
	protected $_school_year='';
	protected $_semester = '';
	
	public function __construct() {
		parent::__construct();
		// automatically load helper & model.
		/*
		$classname = $this->__get_class_name();
		if (is_readable(BASEPATH."../application/helpers/{$classname}_helper.php")) {
			$this->load->helper("{$classname}");
		}
		if (is_readable(BASEPATH."../application/models/{$classname}_model.php")) {
			$this->load->model("{$classname}_model", $classname);
		}
		*/
	}
	
	protected function _get_school_year(){
		$this->db->where('active_semester > ',0);
		$rs = $this->db->get('school_years')->row_array();
		return $rs;
	}
	
	protected function _get_school_year_id(){
		$rs = $this->_get_school_year();
		if(isset($rs) && count($rs)>0){
			return $rs['id'];
		}
		return 0;
	}
	
	protected function _get_school_year_text(){
		$rs = $this->_get_school_year();
		if(isset($rs) && count($rs)>0){
			$school_year = $rs['school_year'];
			return $school_year;
		}
		return 'Tidak ada tahun ajaran yang aktif.';
	}
	
	protected function _get_semester_text(){
		$rs = $this->_get_school_year();
		if(isset($rs) && count($rs)>0){
			$semester = ($rs['active_semester'] % 2 ==0)? "Genap":"Ganjil";
			return " (". $semester . ")";
		}
		return ' Tidak ada semester yang aktif. ';
	}
	
	private function __get_class_name() {
		return strtolower(get_class($this));
	}

	protected function _get_header_data() {
		return array(
			'title' => $this->_title
		);
	}

	//public function _unique_field($value, $fieldname, $id='') {
	public function _unique_field($value, $params) {
		list($fieldname, $id) = explode(',', $params);
		if ( !isset($_POST[$fieldname]) || empty($_POST[$fieldname]) ) {
			$this->form_validation->set_message('_unique_field', "[ERROR:unique_field] Please supply the field name.");
			return FALSE;
		}

		$id = isset($_POST[$id]) ? $_POST[$id] : '';
		$classname = $this->__get_class_name();
		eval('$model = $this->'.$classname.';');
		if (!method_exists($model, 'is_unique_value_on_field')) {
			$this->form_validation->set_message('_unique_field', "[ERROR:unique_field] Please declare method is_unique_value_on_field.");
			return FALSE;
		}else if ($model->is_unique_value_on_field($fieldname, $value, $id)) {
			$this->form_validation->set_message('_unique_field', "Field <em>%s</em> sudah terpakai.");
			return FALSE;
		}
		return TRUE;
	}
	
	public function get_actor_login(){
		return isset($_SESSION['actor'])?$_SESSION['actor']:'';
	}
	
	public function _valid_value($value) {
		if($value<0 || $value>100){
			$this->form_validation->set_message('_valid_value', "Field <em>%s</em> harus berisi angka 0-100.");
			return FALSE;
		}
		return TRUE;
	}

	protected function _get_active_semester_date() {
		$active = $this->school_years->get_active_school_year();
		$data = array();
		switch ($active['active_semester']) {
			case School_Years_Model::ACTIVE_SEMESTER_ODD:
				$data = array('start' => $active['odd_semester_start'], 'end' => $active['odd_semester_end']);
				break;
			case School_Years_Model::ACTIVE_SEMESTER_EVEN:
				$data = array('start' => $active['even_semester_start'], 'end' => $active['even_semester_end']);
				break;
		}
		return $data;
	}

	protected function _get_active_semester_month() {
		$date = $this->_get_active_semester_date();
		$start = extract_sql_date($date['start'], 'm');
		$end   = extract_sql_date($date['end'], 'm');
		if ($end < $start) {
			return array('start' => $end, 'end' => $start);
		}
		return array('start' => $start, 'end' => $end);
	}
}


class Backend_Controller extends MY_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->_school_year_id = $this->_get_school_year_id();
		$this->_school_year = $this->_get_school_year_text();
		$this->_semester = $this->_get_semester_text();
		session_start();
		if (!isset($_SESSION['user']) || empty($_SESSION['user'])) {
			redirect('login');
		}
		$this->load->library('grocery_CRUD');
	}

}


class Base_Student_Controller extends MY_Controller {

	public function __construct() {
		parent::__construct();
		session_start();
		if (!isset($_SESSION['login']) || empty($_SESSION['login'])) {
			redirect('login');
		}
		if (parent::get_actor_login() != 'student') {
			redirect('login/index/1');
		}

		$this->load->model('students_model', 'students');
		$this->load->model('school_years_model', 'school_years');
		$this->load->helper('my_form');
		$this->load->helper('application');
		$this->load->helper('students');
	}

	protected function _get_header_data() {
		$this->load->model('class_grades_model', 'class_grades');
		$this->load->model('classes_model', 'classes');
		$school_year = $this->school_years->get_active_school_year();
		$class 			 = $this->students->get_active_class($_SESSION['id'], $school_year['id']);
		$class_grade = $this->class_grades->get_by_name($this->classes->extract_class_grade($class['name']));
		$data = array('current' => 
			array(
				'student' => array(
					'id' => $_SESSION['id'],
					'name' => $_SESSION['name'],
					'username' => $_SESSION['user'],
				),
				'class' => array(
					'name' => $class['name'],
					'room' => $class['room_code'],
					'id' => $class['id'],
					'absent' => $class['absent'],
					'grade_id' => isset($class_grade['id']) ? $class_grade['id'] : ''
				),
				'school_year' => array(
					'name' => $school_year['school_year'],
					'id' => $school_year['id'],
					'semester_name' => $this->school_years->semester_to_string($school_year['active_semester']),
					'semester_date' => $this->_get_active_semester_date(),
					'semester_month' => $this->_get_active_semester_month(),
				),
			)
		);
		return $data;
	}

}

class Base_Teacher_Controller extends MY_Controller {
	public function __construct() {
		parent::__construct();
		session_start();
		if (!isset($_SESSION['login']) || empty($_SESSION['login'])) {
			redirect('login');
		}
		if (parent::get_actor_login() != 'teacher') {
			redirect('login/index/1');
		}

		$this->load->model('teachings_model','teachings');
		$this->load->model('officials_model','officials');
		$this->load->model('teachers_model','teachers');
		$this->load->model('school_years_model','school_years');
		$this->load->model('classes_model','classes');
		$this->load->model('subjects_model','subjects');
		$this->load->model('students_model','students');

		$this->load->helper('teachers');
	}

	protected function _get_header_data() {
		$school_year = $this->school_years->get_active_school_year();
		$data = array('current' => 
			array(
				'teacher' => array(
					'id' => $_SESSION['id'],
					'name' => $_SESSION['name'],
					'username' => $_SESSION['user'],
				),
				'school_year' => array(
					'name' => $school_year['school_year'],
					'id' => $school_year['id'],
					'semester_name' => $this->school_years->semester_to_string($school_year['active_semester']),
					'semester_date' => $this->_get_active_semester_date(),
					'semester_month' => $this->_get_active_semester_month(),
				),
			)
		);
		return $data;
	}

	protected function _map_students_array($students) {
		$maps = array();
		foreach ($students as $s) {
			$maps[$s['id']] = $s['name'];
		}
		return $maps;
	}

	public function _test_value($value) {
		if ($value >= 0 && $value <= 100) {
			return TRUE;
		}
		$this->form_validation->set_message('_test_value', "Field <em>%s</em> nilainya harus diantara 1 hingga 100.");
		return FALSE;
	}
}

class Base_Librarian_Controller extends MY_Controller {
	public function __construct() {
		parent::__construct();
		session_start();
		if (!isset($_SESSION['login']) || empty($_SESSION['login'])) {
			redirect('login');
		}
		$this->load->model('employees_model', 'employees');
		$header = $this->_get_header_data();
		if (!$this->employees->is_librarian($header['current']['school_year']['id'], 
					$header['current']['librarian']['id'])) {
		//if (parent::get_actor_login() != 'librarian') {
			redirect('login/index/1');
		}


		$this->load->helper('librarians');
	}

	protected function _get_header_data() {
		$school_year = $this->school_years->get_active_school_year();
		$data = array('current' => 
			array(
				'librarian' => array(
					'id' => $_SESSION['id'],
					'name' => $_SESSION['name'],
					'username' => $_SESSION['user'],
				),
				'school_year' => array(
					'name' => $school_year['school_year'],
					'id' => $school_year['id'],
					'semester_name' => $this->school_years->semester_to_string($school_year['active_semester']),
					'semester_date' => $this->_get_active_semester_date(),
					'semester_month' => $this->_get_active_semester_month(),
				),
			)
		);
		return $data;
	}

}
